package com.zxc.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxc.reggie.common.R;
import com.zxc.reggie.entity.Employee;
import com.zxc.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        HttpSession session = request.getSession();

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee employeeData = employeeService.getOne(queryWrapper);
        if (employeeData == null) {
            return R.error("用户不存在");
        }
        if (!password.equals(employeeData.getPassword())) {
            return R.error("账号或密码有误");
        }
        if (employeeData.getStatus() == 0) {
            return R.error("账号已封禁");
        }
        session.setAttribute("employee", employeeData.getId());
        return R.success(employeeData);
    }

    @GetMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save( @RequestBody Employee employee) {

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));


        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page<Employee>> queryPage(int page, int pageSize, String name) {

        Page<Employee> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getUsername, name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    @GetMapping("/{id}")
    public R<Employee> queryInfo(@PathVariable String id) {

        return R.success(employeeService.getById(id));
    }

    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {

        employeeService.updateById(employee);
        return R.success("修改成功");
    }
}
